open Printf;;
open Gl;;
open Vec;;
open Syst;;
open Scene;;

Syst.init () ;;

let squareBuffer = 
  Ogl.makeBuffer 
    ~data:(Ogl.toFloatArray [| -0.5; -0.5; -0.5; 0.5; 0.5; 0.5; 0.5; -0.5 |])
    ~elements:2;;

let prim = 
  Ogl.makePrimitive ~kind:cgl_polygon ~prim:(Ogl.Count 4);;

let state = 
  Ogl.makeRenderState ~vertices:squareBuffer;;

let node = 
  { transform = Matrix.moveForward (Matrix.identity ()) (-50.) ;
    geometry = Ogl.makeGeometryChunk ~state:state ~primitives:[prim];
    children = [] };;

let cam = Camera.make ~fov:60.0 ~aspect:1.0 ~near:0.5 ~far:1000.0;;

glClear cgl_color_buffer_bit;;
glColor3f 1.0 1.0 1.0 ;;

Camera.setProjection cam;;
Camera.viewFrom cam;;

Syst.onKey Sdlkey.KEY_w (fun k -> ignore (Matrix.moveForward cam.Camera.transform 0.01); );;
Syst.onKey Sdlkey.KEY_s (fun k -> ignore (Matrix.moveForward cam.Camera.transform (-0.01)); () );;
Syst.onKey Sdlkey.KEY_a (fun k -> ignore (Matrix.xlate cam.Camera.transform (scale (Matrix.right cam.Camera.transform) 0.01)); );;
Syst.onKey Sdlkey.KEY_d (fun k -> ignore (Matrix.xlate cam.Camera.transform (scale (Matrix.right cam.Camera.transform) (-0.01))); );;

let oooo = Roam.makeOctahedron 1.0;;

while not (Syst.keydown Sdlkey.KEY_ESCAPE) do
  Camera.viewFrom cam;
  renderNode node;
(*  Roam.render oooo;*)
  Syst.pump ();
  glClear cgl_color_buffer_bit ;
  glClear cgl_depth_buffer_bit;
done;;

let out = open_out "gc.txt" in
  Gc.print_stat out;
  close_out out;;

